<template>
  <el-dialog v-model="visible" :title="title">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="6em">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="输入名称" />
      </el-form-item>
      <el-form-item label="跳转链接" prop="skip_path">
        <el-input v-model="form.skip_path" placeholder="输入跳转链接" />
      </el-form-item>
      <el-form-item label="是否有效" prop="is_active" required>
        <el-switch v-model="form.is_active" />
      </el-form-item>
      <el-form-item label="图片" prop="image_url">
        <el-upload
          list-type="picture-card"
          :file-list="fileList"
          :limit="1"
          :auto-upload="false"
          :on-change="handleChange"
          :before-remove="handleRemove"
        >
          <el-icon class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button
          type="primary"
          :loading="bannerStore.loading.create || bannerStore.loading.update"
          @click="handleConfirm"
        >
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { defineModel } from "vue";
import { useBannerStoreHook } from "@/store/modules/system/banner";
import { BannerType } from "@/api/system/banner";
import { UploadFile, UploadFiles } from "element-plus";
import { Plus } from "@element-plus/icons-vue";

const bannerStore = useBannerStoreHook();

export type FormType = "add" | "edit";

export interface Props {
  type: FormType;
  form?: BannerType;
}

const visible = defineModel({ type: Boolean, default: false });

const emits = defineEmits(["confirm"]);

const props = defineProps<Props>();

const formRef = ref();

const form = reactive<Partial<BannerType>>({
  name: "",
  image_url: "",
  is_active: true,
  skip_path: "",
});

const fileList = ref<any>([]);

const rules = {
  name: [
    {
      required: true,
      message: "请输入名称",
      trigger: "change",
    },
  ],
  image_url: [
    {
      required: true,
      message: "请上传图片",
      trigger: "change",
    },
  ],
};

const title = computed(() => {
  return props.type === "add" ? "新增" : "编辑";
});

watch(visible, (val) => {
  if (val) {
    if (props.type === "edit") {
      console.log(props.form);
      fileList.value.push({ uid: Date.now(), url: props.form!.image_url });
      Object.assign(form, props.form);
    }
  } else {
    fileList.value = [];
    form.name = "";
    form.image_url = "";
    form.skip_path = "";
    form.is_active = true;
  }
});

function handleChange(e: UploadFile) {
  console.log(e);
  fileList.value.push(e.raw!);
  form.image_url = URL.createObjectURL(e.raw!);
}

function handleRemove(e: UploadFile, es: UploadFiles) {
  form.image_url = "";
  return true;
}

function handleConfirm() {
  formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      if (props.type === "add") {
        form.image_url = fileList.value[0];
        await bannerStore.fetchCreateBanner(form);
      } else {
        await bannerStore.fetchUpdateBanner(form);
      }
      visible.value = false;
      emits("confirm");
    }
  });
}
</script>

<style scoped lang="scss"></style>
